package com.sgcc.sx.pmos.e_sx_mall.activity;


import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.sgcc.sx.pmos.common.base.BaseActivity;
import com.sgcc.sx.pmos.common.bean.ComputationBean;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.http.MvcNetCallback;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.sx.pmos.common.utils.BigDecimalUtils;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_mall.adapter.ElectricityProportionDataAdapter;
import com.sgcc.sx.pmos.e_sx_mall.bean.ElectricityProportionDataBean;
import com.sgcc.sx.pmos.e_sx_mall.presenter.PredictPriceHttp;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


public class ElectricityProportionDataActivity extends BaseActivity {

    private TextView tvDeviation;
    private TextView tvTotal;
    private TextView tvDeviationValue;
    private RecyclerView rvDataList;
    private TitleBar tb_title;
    private TextView tvSubmit;
    private int pageType;
    private int priceType;
    private ArrayList<String> packageId;
    private ArrayList<String> curveList;
    private ArrayList<String> deviationCurveList;
    private String calDate;
    private ElectricityProportionDataAdapter electricityProportionDataAdapter;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_electricity_proportion_data;
    }

    @Override
    protected void initViews() {
        super.initViews();
        tvDeviation = findViewById(R.id.tvDeviation);
        tb_title = findViewById(R.id.tb_title);
        tvTotal = findViewById(R.id.tvTotal);
        tvDeviationValue = findViewById(R.id.tvDeviationValue);
        rvDataList = findViewById(R.id.rvDataList);
        tvSubmit = findViewById(R.id.tvSubmit);
        tb_title.setTitle("设置电量比例");
        tb_title.setRightIcon(R.mipmap.go_main, view -> goMainActivity());

    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        pageType = intent.getIntExtra("pageType", 0);
        priceType = intent.getIntExtra("priceType", 1);
        calDate = intent.getStringExtra("calDate");
        packageId = intent.getStringArrayListExtra("packageId");
        curveList = intent.getStringArrayListExtra("curveList");
        deviationCurveList = intent.getStringArrayListExtra("deviationCurveList");
        tvDeviation.setVisibility(pageType == 1 ? View.GONE : View.VISIBLE);
        tvDeviationValue.setVisibility(pageType == 1 ? View.GONE : View.VISIBLE);

        int size = null == curveList || curveList.size() == 0 ? 24 : curveList.size();

        List<ElectricityProportionDataBean> list = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            list.add(new ElectricityProportionDataBean(i < 9 ? "0" + (i + 1) : "" + (i + 1),
                    null == curveList || curveList.size() == 0 ? "0.00" : curveList.get(i),
                    null == deviationCurveList || deviationCurveList.size() == 0 ? "0.00" : deviationCurveList.get(i)));
        }

        rvDataList.setLayoutManager(new LinearLayoutManager(mContext));
        electricityProportionDataAdapter = new ElectricityProportionDataAdapter(mContext, list, pageType == 1);

        rvDataList.setAdapter(electricityProportionDataAdapter);

        total(list);
        electricityProportionDataAdapter.setOnEditInputCallBack(new ElectricityProportionDataAdapter.OnEditInputCallBack() {
            @Override
            public void onCurve() {
                total(electricityProportionDataAdapter.getData());
            }

        });

        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String total = tvTotal.getText().toString().trim();
                double v = Double.parseDouble(total);
                if (v != 1) {
                    ToastUtils.showShort("各时段用电比例和为1");
                    return;
                }
                getPrice();


            }
        });

    }


    private void total(List<ElectricityProportionDataBean> list) {
        double total = 0;

        for (ElectricityProportionDataBean electricityProportionDataBean : list) {
            total += Double.parseDouble(electricityProportionDataBean.getCurve());
        }
        tvTotal.setText(BigDecimalUtils.onKeep2Decimal_new(total + ""));
    }


    private void getPrice() {

        // subEnergyType        第一位 分时1不分时0，第二位 约定1不约定0

        String subEnergyType = "11";
        List<String> realEnergyRatioList = new ArrayList<>();
        List<String> agreeEnergyRatioList = new ArrayList<>();
        List<String> deviationRatioList = new ArrayList<>();
        if (pageType == 0) {// 0分时 约定电量  1 分时 不约定电量  2不分时 约定电量  3 不分时 不约定电量
            subEnergyType = "11";
//            deviationRatioList = deviationCurveList;
//            agreeEnergyRatioList = curveList;
            for (ElectricityProportionDataBean datum : electricityProportionDataAdapter.getData()) {
                deviationRatioList.add(datum.getDeviationCurve());
                agreeEnergyRatioList.add(datum.getCurve());
            }
        }
        if (pageType == 1) {
            subEnergyType = "10";
//            realEnergyRatioList = curveList;
            for (ElectricityProportionDataBean datum : electricityProportionDataAdapter.getData()) {
                realEnergyRatioList.add(datum.getCurve());
            }
        }


        PredictPriceHttp.price(packageId, calDate, priceType, subEnergyType, "", realEnergyRatioList,
                agreeEnergyRatioList, deviationRatioList, new MvcNetCallback<ComputationBean>() {
                    @Override
                    public void onResponse(ComputationBean response, int id) {
                        super.onResponse(response, id);
                        Object data = response.getData();

                        if (data instanceof Collection) {
                            List<ComputationBean.Bean> beanList = GsonUtils.fromJson(GsonUtils.toJson(data), new TypeToken<List<ComputationBean.Bean>>() {
                            }.getType());
                            EventBus.getDefault().post(new EventCenter<>(ArguConstant.REFRESH_ESTIMATED_PRICE, beanList));
                            ActivityUtils.finishActivity(ElectricityProportionActivity.class);
                            finish();
                        } else if (data instanceof String) {
                            ToastUtils.showShort((String) data);
                        }
                    }
                });
    }


}